package com.fz.song.web.service.impl;


import com.fz.song.web.entity.User;
import com.fz.song.web.entity.UserMsg;
import com.fz.song.web.service.IUserMsgService;
import com.fz.song.web.service.IUserService;
import com.fz.song.web.service.MqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Slf4j
public class MqServiceImpl implements MqService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserMsgService userMsgService;


    /**
     * 添加事务
     * UserMsg表添加唯一索引,
     * 相同的消息id会回滚事务
     *
     * @param user
     * @param msgId
     */
    @Override
    @Transactional
    public void saveUser(User user, String msgId) {
        //消息表个业务表在一个事务中,消息表消息id有唯一索引,在重复的时候会异常,回滚事务
        UserMsg msg = new UserMsg();
        msg.setMsgId(msgId);
        userMsgService.save(msg);
        userService.save(user);

    }
}
